package com.zy.config;

import com.zy.ai.guardrail.PromptSafetyInputGuardrail;
import com.zy.ai.service.TestService;
import com.zy.ai.service.WordService;
import com.zy.ai.tools.NodeTools;
import com.zy.ai.tools.QuestionTools;
import com.zy.ai.tools.TestTools;
import dev.langchain4j.community.store.memory.chat.redis.RedisChatMemoryStore;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @BelongsProject: SmartBidAI
 * @BelongsPackage: com.zy.config
 * @Author: zhangyan
 * @CreateTime: 2025-09-01  20:35
 * @Description: TODO
 * @Version: 1.0
 */
@Configuration
public class AiServiceConfig {
    @Resource
    private OpenAiChatModel chatModel;
    @Resource
    private StreamingChatModel streamingChatModel;
    @Resource
    private RedisChatMemoryStore redisChatMemoryStore;
    @Resource
    private TestTools testTools;
    @Resource
    private NodeTools nodeTools;
    @Resource
    private QuestionTools questionTools;

//    @Bean
//    public EmbeddingModel embeddingModel(QwenPlusEmbeddingClient client) {
//        return new EmbeddingModel() {
//            @Override
//            public Response<Embedding> embed(TextSegment textSegment) {
//                List<Double> vector = client.getEmbedding(textSegment.text());
//                float[] vec = new float[vector.size()];
//                for (int i = 0; i < vector.size(); i++) {
//                    vec[i] = vector.get(i).floatValue();
//                }
//                return Response.from(Embedding.from(vec));
//            }
//
//            @Override
//            public Response<List<Embedding>> embedAll(List<TextSegment> textSegments) {
//                throw new UnsupportedOperationException("暂未实现批量 embed");
//            }
//        };
//    }

    @Bean
    public TestService testService(){
            ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder()
                    .id(memoryId)
                    .maxMessages(20)
                    .chatMemoryStore(redisChatMemoryStore)
                    .build();
        return AiServices.builder(TestService.class)
                .streamingChatModel(streamingChatModel)
                .chatMemoryProvider(chatMemoryProvider)
                .tools(testTools,nodeTools,questionTools)
                .inputGuardrails(new PromptSafetyInputGuardrail())
                .build();
    }
    @Bean
    public WordService wordService(){
        ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(20)
                .chatMemoryStore(redisChatMemoryStore)
                .build();
        return AiServices.builder(WordService.class)
                .chatModel(chatModel)
                .chatMemoryProvider(chatMemoryProvider)
                .tools(testTools,nodeTools)
                .inputGuardrails(new PromptSafetyInputGuardrail())
                .build();
    }
}
